package com.example.demo.entity;

import com.example.demo.enums.OrderPriorityEnum;
import com.example.demo.enums.OrderStatusEnum;
import com.example.demo.enums.OrderTypeEnum;
import com.example.demo.enums.PaymentMethodEnum;
import lombok.Data;
import org.hibernate.annotations.CreationTimestamp;
import org.hibernate.annotations.UpdateTimestamp;

import javax.persistence.*;
import javax.validation.constraints.DecimalMin;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.math.BigDecimal;
import java.time.LocalDateTime;

/**
 * 订单实体
 * 
 * 演示功能：
 * 1. 枚举字段自动转换为数据库数值类型存储
 * 2. JSON字段自动序列化和反序列化
 * 3. 所有枚举操作零配置，开箱即用
 */
@Entity
@Table(name = "orders")
@Data
public class OrderEntity {
    
    /**
     * 订单ID
     */
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    
    /**
     * 订单编号
     */
    @Column(name = "order_no", unique = true, nullable = false, length = 32)
    @NotBlank(message = "订单编号不能为空")
    private String orderNo;
    
    /**
     * 用户ID
     */
    @Column(name = "user_id", nullable = false)
    @NotNull(message = "用户ID不能为空")
    private Long userId;
    
    /**
     * 订单状态 - 框架自动转换为TINYINT存储
     */
    @Column(name = "status", nullable = false)
    @NotNull(message = "订单状态不能为空")
    private OrderStatusEnum status;
    
    /**
     * 订单类型 - 框架自动转换为TINYINT存储
     */
    @Column(name = "type", nullable = false)
    @NotNull(message = "订单类型不能为空")
    private OrderTypeEnum type;
    
    /**
     * 订单优先级 - 框架自动转换为TINYINT存储
     */
    @Column(name = "priority", nullable = false)
    @NotNull(message = "订单优先级不能为空")
    private OrderPriorityEnum priority;
    
    /**
     * 支付方式 - 框架自动转换为TINYINT存储
     */
    @Column(name = "payment_method")
    private PaymentMethodEnum paymentMethod;
    
    /**
     * 订单总金额
     */
    @Column(name = "total_amount", nullable = false, precision = 10, scale = 2)
    @NotNull(message = "订单金额不能为空")
    @DecimalMin(value = "0.01", message = "订单金额必须大于0")
    private BigDecimal totalAmount;
    
    /**
     * 实付金额
     */
    @Column(name = "paid_amount", precision = 10, scale = 2)
    private BigDecimal paidAmount;
    
    /**
     * 收货地址
     */
    @Column(name = "shipping_address", length = 500)
    private String shippingAddress;
    
    /**
     * 收货人姓名
     */
    @Column(name = "receiver_name", length = 100)
    private String receiverName;
    
    /**
     * 收货人电话
     */
    @Column(name = "receiver_phone", length = 20)
    private String receiverPhone;
    
    /**
     * 订单备注
     */
    @Column(name = "remark", length = 1000)
    private String remark;
    
    /**
     * 支付时间
     */
    @Column(name = "paid_time")
    private LocalDateTime paidTime;
    
    /**
     * 发货时间
     */
    @Column(name = "shipped_time")
    private LocalDateTime shippedTime;
    
    /**
     * 完成时间
     */
    @Column(name = "completed_time")
    private LocalDateTime completedTime;
    
    /**
     * 创建时间
     */
    @CreationTimestamp
    @Column(name = "created_time", nullable = false, updatable = false)
    private LocalDateTime createdTime;
    
    /**
     * 更新时间
     */
    @UpdateTimestamp
    @Column(name = "updated_time", nullable = false)
    private LocalDateTime updatedTime;
    
    /**
     * 业务方法：检查订单是否可以取消
     */
    public boolean canCancel() {
        return this.status != null && this.status.canCancel();
    }
    
    /**
     * 业务方法：检查订单是否可以退款
     */
    public boolean canRefund() {
        return this.status != null && this.status.canRefund();
    }
    
    /**
     * 业务方法：检查是否为在线支付
     */
    public boolean isOnlinePayment() {
        return this.paymentMethod != null && this.paymentMethod.isOnlinePayment();
    }
}
